package com.hcb.tonguetiphealth.view;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.adapter.ChatListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.manager.ChatManager;
import com.hcb.tonguetiphealth.model.Chat;
import com.hcb.tonguetiphealth.model.User;
import com.hcb.tonguetiphealth.util.StringUtils;

public class ChatHistoryActivity extends Activity {
	private TextView tv_name;
	private ImageView iv_img;
	private Button bt_deleteChat;
	private ListView listView;
	private ChatListAdapter adapter;
	private ChatManager chatManager;
	private List<Chat> chatList;
	private User user;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage_personal_chat_history);
		
		iv_img = (ImageView)findViewById(R.id.homepage_personal_chat_current_member_img);
		tv_name = (TextView)findViewById(R.id.homepage_personal_chat_current_member_name);
		bt_deleteChat = (Button)findViewById(R.id.homepage_personal_chat_clear_button);
		listView = (ListView)findViewById(R.id.homepage_personal_chat_listview);
		bt_deleteChat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showClearDialog();
			}
		});
      
		user = (User)getIntent().getSerializableExtra("user");
		chatManager = new ChatManager(this);
		chatList = chatManager.getChatList(user, true);
		adapter = new ChatListAdapter(this);
		listView.setAdapter(adapter);
		
		
		Bitmap bitmap = AsyncBitmapLoader.getInstance().loadBitmap(iv_img, user.getImgUrl(), new ImageCallBack() {  
            
            public void imageLoad(ImageView imageView, Bitmap bitmap) {  
                // TODO Auto-generated method stub  
            	if(bitmap == null) {
            		imageView.setImageResource(R.drawable.ic_launcher); 
            	} else {
            		imageView.setImageBitmap(bitmap);  
            	}
            }  
        });  
		if(bitmap == null) {    
			iv_img.setImageResource(R.drawable.ic_launcher);    
		} else {    
			iv_img.setImageBitmap(bitmap);    
		}
		String name = user.getRealName();
		name = StringUtils.isEmpty(name)? "" : " (" + name + ")";
		tv_name.setText(user.getUserName() + name);
		adapter.setMsgs(chatList);
	}
	protected void showClearDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(user.getUserName());
		//builder.setIcon(R.drawable.what);
		builder.setMessage("确定要清空聊天记录？");
		builder.setCancelable(true);
		builder.setPositiveButton("清空", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				deleteChat();
				if(Global.homepageChatWidget2 != null) {
					Global.homepageChatWidget2.notifyAdapter();
				}
				startActivity(new Intent(ChatHistoryActivity.this, ChatActivity.class));
				ChatHistoryActivity.this.finish();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}
	private void deleteChat() {
		new ChatManager(this).deleteChat(user.getUserId());
		user.getChatList().clear();
		for(User member : Global.memberList) {
			if(member.getUserName().equals(user.getUserName())) {
				member.getChatList().clear();
			}
		}
	}
	@Override
	public void onBackPressed() {
		startActivity(new Intent(this, ChatActivity.class));
		super.onBackPressed();
	}
	
}